using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Scenes;
using Barrage.Global;
using Barrage.GUI.Elements;

namespace Barrage.GUI
{
    /// <summary>
    /// A GUISection is a GameSection that loads a GUILayout from a file in the Layout folder, and uses it as the main scene.
    /// It also implements some logic to allow user interaction with the scene, and some helper functions
    /// </summary>
    public abstract class GUISection:GameSection
    {
        GUILayout layout;
        /// <summary>
        /// GUILayout scene that holds GUI data of this section
        /// </summary>
        public GUILayout Layout { get { return layout; } }

        /// <summary>
        /// Constructor. Inits a section with the layout specified
        /// </summary>
        /// <param name="layout">layout's name</param>
        /// <param name="parent">Game that will update this scene</param>
        public GUISection(string layout,Game parent):base(parent)
        {
            this.layout = GUILayout.Load(layout);
        }

        /// <summary>
        /// \internal Adds the loaded layout to the scene list
        /// </summary>
        protected override void initScenes()
        {
            AddScene(this.layout);
        }

        /// <summary>
        /// \internal The main loop of this section checks for keyboard / gamepad interaction and updates the layout based on that input
        /// </summary>
        public override void MainLoop()
        {
            if (Keyboard.Instance.KeyReleased(SlimDX.DirectInput.Key.UpArrow) || Gamepad.AnyButtonPressed(SlimDX.XInput.GamepadButtonFlags.DPadUp)
                || Gamepad.AnyStickPressed(Stick.LeftStick, StickDirection.Up))
                layout.SelectPreviousItem();
            if (Keyboard.Instance.KeyReleased(SlimDX.DirectInput.Key.DownArrow) || Gamepad.AnyButtonPressed(SlimDX.XInput.GamepadButtonFlags.DPadDown)
                || Gamepad.AnyStickPressed(Stick.LeftStick,StickDirection.Down))
                layout.SelectNextItem();
            if (Keyboard.Instance.KeyReleased(SlimDX.DirectInput.Key.Return) || Gamepad.AnyButtonReleased(SlimDX.XInput.GamepadButtonFlags.Start|SlimDX.XInput.GamepadButtonFlags.A))
                layout.SelectedItem.Interact();
        }
        /// <summary>
        /// Utility function that registers a GUIButton element of the layout with the GUIButtonHandler specified
        /// </summary>
        /// <param name="id">Button id</param>
        /// <param name="handler">Handler for the OnInteraction event</param>
        protected void RegisterButtonEvent(string id,GUIButtonHandler handler){
            try{
                ((GUIButton)layout.Elements[id]).OnInteraction += handler;
            }
            catch(Exception)
            {
            }
        }

        /// <summary>
        /// Utility function that unregisters a GUIButton element of the layout with the GUIButtonHandler specified
        /// </summary>
        /// <param name="id">Button id</param>
        /// <param name="handler">Handler for the OnInteraction event</param>
        protected void UnRegisterButtonEvent(string id, GUIButtonHandler handler)
        {
            try
            {
                ((GUIButton)layout.Elements[id]).OnInteraction -= handler;
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// \internal Clear resources
        /// </summary>
        public override void Dispose()
        {
            layout = null;
            base.Dispose();
        }
    }
}
